package com.huajin.codetest.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * volatile
 * 底层
 */
public interface VolatileTest {
	
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newFixedThreadPool(4);
		executorService.execute(() -> new VolatileExample().writer()); 
		executorService.execute(() -> new VolatileExample().reader());
	}

}

class VolatileExample {
	  int x = 0;
	  volatile boolean v = false;
	  public void writer() {
	    x = 42;
	    v = true;
	  }
	  public void reader() {
	    if (v == true) {
	      System.out.println(x);
	    } else {
	    	System.out.println(x);
	    }
	  }
	}
